package com.huike.report.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huike.business.mapper.TbBusinessMapper;
import com.huike.clues.domain.TbAssignRecord;
import com.huike.clues.domain.vo.IndexStatisticsVo;
import com.huike.clues.mapper.SysDeptMapper;
import com.huike.clues.mapper.SysDictDataMapper;
import com.huike.clues.mapper.TbActivityMapper;
import com.huike.clues.mapper.TbAssignRecordMapper;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.common.core.domain.entity.SysDept;
import com.huike.common.utils.SecurityUtils;
import com.huike.contract.mapper.TbContractMapper;
import com.huike.report.domain.vo.IndexVo;
import com.huike.report.mapper.ReportMapper;
import com.huike.report.service.IReportService;

@Service
public class ReportServiceImpl implements IReportService {

    @Autowired
    private TbContractMapper contractMapper;


    @Autowired
    private SysDictDataMapper sysDictDataMapper;


    @Autowired
    private TbClueMapper clueMapper;

    @Autowired
    private TbActivityMapper activityMapper;

    @Autowired
    private TbBusinessMapper businessMapper;

    @Autowired
    private SysDeptMapper deptMapper;

    @Autowired
    private TbAssignRecordMapper assignRecordMapper;
    
    @Autowired
    private ReportMapper reportMpper;

    /**
     * 线索
     * @param request
     * @return
     */
    @Override
    public IndexVo getIndex(IndexStatisticsVo request) {
        Long deptId= request.getDeptId();
        TbAssignRecord tbAssignRecord=new TbAssignRecord();
        tbAssignRecord.setLatest("1");
        assignRecordMapper.selectAssignRecordList(tbAssignRecord);
        return null;
    }

    /**
     * 合同
     * @param request
     * @return
     */
    @Override
    public List<Map<String, Object>> salesStatisticsForIndex(IndexStatisticsVo request) {
        List<Map<String, Object>> list= contractMapper.contractStatisticsByUser(request);
        for (Map<String, Object> datum : list) {
            Long deptId= (Long) datum.get("dept_id");
            if(deptId!=null){
                SysDept dept= deptMapper.selectDeptById(deptId);
                datum.put("deptName", dept.getDeptName());
            }
        }
        return list;
    }

    @Override
    public Map getVulnerabilityMap(String beginCreateTime, String endCreateTime) {
        Map<String, Integer> map = new HashMap<>();
        //线索数  线索表的所有数据  根据条件
        map.put("cluesNums",clueMapper.countAllClues(beginCreateTime, endCreateTime));
        //有效线索数
        map.put("effectiveCluesNums",clueMapper.effectiveCluesNums(beginCreateTime, endCreateTime));
        //商机数
        map.put("businessNums",businessMapper.businessNums(beginCreateTime, endCreateTime));
        //合同数
        map.put("contractNums",contractMapper.contractNums(beginCreateTime, endCreateTime));

        return map;
    }


    //线索转化龙虎榜接口
    @Override
    public List<Map<String, Object>> clueChangeStatisticsForIndex(IndexStatisticsVo request) {
        //统计所有的线索
        int allclues = clueMapper.countAllClues(request.getBeginCreateTime() + " 00:00:00",
                request.getEndCreateTime() + " 23:59:59");
        //根据用户查询线索(原代码中已有该方法，直接使用)
        List<Map<String, Object>> list = clueMapper.countAllClueByUser(request);
        //计算转换率
        for (Map<String, Object> datum : list) {
            BigDecimal radio = getRadio(allclues, (Long) datum.get("num"));
            datum.put("radio", radio);
        }
        return list;
    }

    //计算转换率(原代码中已有该方法，直接使用)
    private BigDecimal getRadio(Integer all, Long num) {
        if (all.intValue() == 0) {
            return new BigDecimal(0);
        }
        BigDecimal numBigDecimal = new BigDecimal(num);
        BigDecimal allBigDecimal = new BigDecimal(all);
        BigDecimal divide = numBigDecimal.divide(allBigDecimal, 4, BigDecimal.ROUND_HALF_UP);
        return divide.multiply(new BigDecimal(100));
    }

    //商机转换龙虎榜
    @Override
    public List<Map<String, Object>> businessChangeStatisticsForIndex(IndexStatisticsVo request) {
        //查询所有的商机
        int allBusiness = businessMapper.countAllBusiness(request.getBeginCreateTime() + " 00:00:00",
                request.getEndCreateTime() + " 23:59:59");

        List<Map<String, Object>> list = businessMapper.countAllContractByUser(request);
        for (Map<String, Object> datum : list) {
            BigDecimal radio = getRadio(allBusiness, (Long) datum.get("num"));
            datum.put("radio", radio);
        }
        return list;
    }

    //首页数据统计
    @Override
    public Map<String, Object> getcontractsBasicInfo(IndexStatisticsVo request, String now) {
        Map<String, Object> result = new HashMap<String,Object>();
        String username = SecurityUtils.getUsername();

        Map baseData = reportMpper.getBaseData(request, now, username);
        Map todayData = reportMpper.getTodayData(request, now, username);
        Map todoData = reportMpper.getTodoData(request, now, username);
        result.putAll(todayData);
        result.putAll(baseData);
        result.putAll(todoData);
        if(!username.equals("admin")) {
            //不是admin 不能看见待分配商机和待分配线索
            result.put("toallocatedBusinessNum", 0);
            result.put("toallocatedCluesNum", 0);
        }
        return result;
    }

}